<?

require_once('./misc.php');
	global $db, $yap, $jackknife;

$NUM_PER_PAGE = 10;
$CUR_PAGE = $_SERVER['PHP_SELF']."?p={$p}";

function print_mail($array,$n){
echo "	<tr class='{$n}' onclick=\"$('#{$array['messageID']}').toggle(800)\">\n
		<td>{$array['sentDate']}</td>\n
		<td>{$array['senderID']}<br />\n
		<span id='corp'>{$array['corporationName']}</span> <br />\n
		<span id='ally'>{$array['allianceName']}</span>\n
		</td>\n
		<td>{$array['title']}</td>\n
		<td>{$array['toCharacters']}</td>\n
		<td>{$array['toCorpOrAlliance']}</td>\n
		<td>{$array['toList']}</td>\n</tr>\n<tr style='display:none;' id='{$array['messageID']}'>\n<td class='message' colspan=6>\n<hr />\n{$array['message']}\n<hr />\n</td>\n</tr>\n";
}
	$orderby = (isset($_REQUEST['orderby']))?mres($_REQUEST['orderby']):"sentDate";
	$ascendby = (isset($_REQUEST['ascendby']))?mres($_REQUEST['ascendby']):"DESC";
	
	$s_player = (isset($_REQUEST['plr']))?$_REQUEST['plr']:false;
	$s_corp = (isset($_REQUEST['corp']))?$_REQUEST['corp']:false;
	$s_ally = (isset($_REQUEST['ally']))?$_REQUEST['ally']:false;
	//$s_list = (isset($_REQUEST['list']))?$_REQUEST['list']:false;
	$s_player = ($s_player!="Player..")?mres($s_player):false;
	$s_corp = ($s_corp!="Corporation..")?mres($s_corp):false;
	$s_ally = ($s_ally!="Alliance..")?mres($s_ally):false;
	//$s_list = ($s_list!="List..")?mres($s_list):false;
	
	$searchby = false;
	$searchtext = "";
	// if ($s_list){
		// $searchby = "toList";
		// $searchtext = $s_list;
	// }
	if ($s_ally){
		$searchby = "allianceName";
		$searchtext = $s_ally;
	}
	if ($s_corp){
		$searchby = "corporationName";
		$searchtext = $s_corp;
	}
	if ($s_player){
		$searchby = "senderID";
		$searchtext = $s_player;
	}
	if ($searchby) {
	$where = "WHERE $searchby='$searchtext'";
	} else $where="";
	
	$page = (isset($_REQUEST['page']))?$_REQUEST['page']:1;

	$query_list_mails = mysql_query('SELECT * FROM api_mail_list', $jackknife);
	$total_mails = mysql_num_rows($query_list_mails);
	$total_pages = ceil(($total_mails/$NUM_PER_PAGE));
	
	$query_all_mail = mysql_query("SELECT * FROM api_mail_list {$where} ORDER BY {$orderby} {$ascendby} LIMIT ".(($page*$NUM_PER_PAGE)-$NUM_PER_PAGE).",".$NUM_PER_PAGE, $jackknife);
	while($data = mysql_fetch_assoc($query_all_mail)) {
		$mail_messages[] = $data;
	}
?>
<form action="<?=$CUR_PAGE;?>"> 
	<input name="p" type="hidden" value="<?=$p;?>"> SEARCH : 
	<input id="player_search" type="text" name="plr" onfocus="if(this.value=='Player..')plr.value='';corp.value='';ally.value='';" value="Player.." /> 
	<input id="corp_search" type="text" name="corp" onfocus="if(this.value=='Corporation..')plr.value='';corp.value='';ally.value='';" value="Corporation.." /> 
	<input id="ally_search" type="text" name="ally" onfocus="if(this.value=='Alliance..')plr.value='';corp.value='';ally.value='';" value="Alliance.." /> 
	<!--<input id="list_search" type="text" name="list" onfocus="if(this.value=='List..')this.value='';" value="List.." /> 
	<div id="list_suggest"></div>-->
	<input id="search_btn" type="submit" value="Search" /> 
</form>
<table>
	<thead>
		<tr>
			<td width="130px">
				<a href='<?=$CUR_PAGE;?>&orderby=sentDate&ascendby=<?=($ascendby=="ASC")?"DESC":"ASC";?>'>Sent</a>
			</td>
			<td width=130px>
				<a href='<?=$CUR_PAGE;?>&orderby=senderID&ascendby=<?=($ascendby=="ASC")?"DESC":"ASC";?>'>From</a>
				(<a href='<?=$CUR_PAGE;?>&orderby=corporationName&ascendby=<?=($ascendby=="ASC")?"DESC":"ASC";?>'>Corp</a>)
				(<a href='<?=$CUR_PAGE;?>&orderby=allianceName&ascendby=<?=($ascendby=="ASC")?"DESC":"ASC";?>'>Ally</a>)
			</td>
			<td width=200px>
				<a href='<?=$CUR_PAGE;?>&orderby=title&ascendby=<?=($ascendby=="ASC")?"DESC":"ASC";?>'>Title</a>
			</td>
			<td width="130px">
				<a href='<?=$CUR_PAGE;?>&orderby=toCharacters&ascendby=<?=($ascendby=="ASC")?"DESC":"ASC";?>'>To Char</a>
			</td>
			<td width=130px>
				<a href='<?=$CUR_PAGE;?>&orderby=toCorpOrAlliance&ascendby=<?=($ascendby=="ASC")?"DESC":"ASC";?>'>To Corp/Ally</a>
			</td>
			<td width=130px>
				<a href='<?=$CUR_PAGE;?>&orderby=toList&ascendby=<?=($ascendby=="ASC")?"DESC":"ASC";?>'>List</a>
			</td>
		</tr>
	</thead>
	<?
	$n=0;
	if (isset($mail_messages)) {
		foreach($mail_messages as $mail) {
		$n++;
		if ($n==1)
			print_mail($mail,"qrg");
		else
			print_mail($mail,"normal");
		if ($n==2) $n=0;
		}
	}
	?>
</table>
<?
$bpage = (($page-1)<1)?1:$page-1;
$npage = (($page+1)>$total_pages)?$total_pages:$page+1;
echo "<span> <a href=?p=mail&page={$bpage}> Prev</a> Page {$page} of {$total_pages} <a href=?p=mail&page={$npage}>Next</a></span><br />Managed By Yapeal API.";
?>
Managed By ALE Api